package com.example.youhuo.mvp.model

import android.util.Log
import com.example.mvp.model.BaseModel
import com.example.myapplication.entity.*
import com.example.youhuo.entity.Banner_Entity
import com.example.youhuo.entity.BaseEntity
import com.example.youhuo.entity.Tab_Entity
import com.example.youhuo.mvp.contract.LjlContract
import com.example.youhuo.network.Api
import com.example.youhuo.network.HttpType
import com.example.youhuo.network.NetWorkManager
import io.reactivex.Observable
import io.reactivex.functions.Function
import kotlin.math.log


class LjlModel : BaseModel(),LjlContract.LjlModel  {



     override fun getData(params: ArrayList<Map<String, Any>>): Observable<BaseEntity> {
         var ob:Observable<BaseEntity>? = null
         var i = 0
         var obs:ArrayList<Observable<BaseEntity>> = ArrayList()

         while (i<params.size){
            var map:Map<String,Any> = params.get(i)
             var code = map.get("code")

             when(code){
                HttpType.LISTCODE->{
                    var tabData = NetWorkManager.instance.retrofit!!.create(Api::class.java)
                        .getTabData(map2json(map))
                    var map1:Observable<BaseEntity> = tabData.map(object : Function<Tab_Entity, BaseEntity> {
                        override fun apply(t: Tab_Entity): BaseEntity {
                            return t
                        }
                    })
                    obs.add(map1)
                }
                 HttpType.BANNER->{
                     var bannerdata = NetWorkManager.instance.retrofit!!.create(Api::class.java)
                         .getBanner()
                     var map1 = bannerdata.map(object : Function<Banner_Entity, BaseEntity> {
                         override fun apply(t: Banner_Entity): BaseEntity {
                             return t
                         }

                     })
                     obs.add(map1)

                 }
                 HttpType.FENLEI->{
                     var getfenlei = NetWorkManager.instance.retrofit!!.create(Api::class.java)
                         .getfenlei(map2json(map))
                     var map1:Observable<BaseEntity> = getfenlei.map(object : Function<Fenlei_Entity, BaseEntity> {
                         override fun apply(t: Fenlei_Entity): BaseEntity {
                             return t
                         }
                     })
                     obs.add(map1)
                 }
                 HttpType.FENLEIImage->{
                     var getfenleiImg = NetWorkManager.instance.retrofit!!.create(Api::class.java)
                         .getfenleiImg(map2json(map))
                     var map1 = getfenleiImg.map(object : Function<Clothing_Entity, BaseEntity> {
                         override fun apply(t: Clothing_Entity): BaseEntity {
                             return t
                         }

                     })
                     obs.add(map1)

                 }
                 HttpType.DETAILS->{
                     var getdetailsdata = NetWorkManager.instance.retrofit!!.create(Api::class.java)
                         .getdetailsdata(map2json(map))
                     var map1 = getdetailsdata.map(object : Function<Details_Entity, BaseEntity> {
                         override fun apply(t: Details_Entity): BaseEntity {
                             return t
                         }
                     })
                     obs.add(map1)
                 }
                 HttpType.REGISTER->{
                     var register = NetWorkManager.instance.retrofit!!.create(Api::class.java)
                         .Register(map2json(map))
                     var map1 = register.map(object : Function<Regist_Entity, BaseEntity> {
                         override fun apply(t: Regist_Entity): BaseEntity {
                             return t
                         }

                     })
                     obs.add(map1)

                 }
                 HttpType.LOGIN->{
                     var login = NetWorkManager.instance.retrofit!!.create(Api::class.java)
                         .Login(map2json(map))
                     var map1 = login.map(object : Function<Login_Entity, BaseEntity> {
                         override fun apply(t: Login_Entity): BaseEntity {
                             return t
                         }

                     })
                     obs.add(map1)
                 }
                 HttpType.ADDCAR->{
                     var addCar = NetWorkManager.instance.retrofit!!.create(Api::class.java)
                         .AddCar(map2json(map))
                     var map1 = addCar.map(object : Function<BaseEntity, BaseEntity> {
                         override fun apply(t: BaseEntity): BaseEntity {
                             return t
                         }
                     })
                     obs.add(map1)
                 }
             }

             i++
         }

       ob = Observable.merge(obs)
         return ob

     }


}